﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "NotifyService" in code, svc and config file together.
public class NotifyService : INotifyService
{
    // count number of unread message
    public int CountNewMessage(String userName)
	{
        DataClassesDataContext db = new DataClassesDataContext();
        int count = 0;
        try
        {
            count = db.Community_Tables.Where(q => q.User_Table1.Username.Equals(userName) && q.Mess_Status.Equals(false)).ToList().Count;
        }
        catch
        {
            count = 0;
        }
        return count;
	}

    // check time-expired of all item
    public void CheckTimeOut()
    {
        DataClassesDataContext db = new DataClassesDataContext();
        var timeOutItems = db.Item_Tables.Where(q => q.Bid_EndDat.Value > DateTime.Now && q.Bid_Status == true);
        foreach (Item_Table item in timeOutItems)
        {
            System.Data.Common.DbTransaction trans = null;
            try
            {
                //trans = db.Connection.BeginTransaction();
                //db.Transaction = trans;
                item.Bid_Status = false;
                Auction_Table currentAction = FindCurrentAuction(item.Item_ID);
                Community_Table shellMessage = new Community_Table();
                // set sheller message
                shellMessage.Mess_ID = Guid.NewGuid();
                shellMessage.Mess_Status = false;
                shellMessage.Mess_Time = DateTime.Now;
                shellMessage.User_Send = default(Guid);
                if (currentAction != null)
                {
                    shellMessage.User_Receive = (Guid)currentAction.Item_Table.UserSell_ID;
                    shellMessage.Mess_Content = "Your " + item.Item_Title + " shell for " + currentAction.User_Table.Username + " whith price: " + currentAction.Bid_Amount;

                    // set buyer message
                    Community_Table buyMessage = new Community_Table();
                    buyMessage.Mess_ID = Guid.NewGuid();
                    buyMessage.User_Send = default(Guid);
                    buyMessage.User_Receive = currentAction.UserID;
                    buyMessage.Mess_Status = false;
                    buyMessage.Mess_Time = DateTime.Now;
                    buyMessage.Mess_Content = "You sold " + item.Item_Title + " whith price: " + currentAction.Bid_Amount;
                    db.Community_Tables.InsertOnSubmit(buyMessage);
                }
                else
                {
                    shellMessage.User_Receive = (Guid)item.UserSell_ID;
                    shellMessage.Mess_Content = "no one buy your " + item.Item_Title ;
                }
                db.Community_Tables.InsertOnSubmit(shellMessage);
                db.SubmitChanges();
                //trans.Commit();
            }
            catch
            {
                return;
            }
        }
    }

    // find current auction
    private Auction_Table FindCurrentAuction(Guid itemID)
    {
        DataClassesDataContext db = new DataClassesDataContext();
        Auction_Table currentAuction = null;
        try
        {
            if (db.Auction_Tables.Where(q => q.ItemID.Equals(itemID)).Any())
            {
                currentAuction = db.Auction_Tables.Where(q => q.ItemID.Equals(itemID)).OrderByDescending(q => q.Bid_Time).First();
                return currentAuction;
            }
            else
            {
                return null;
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    public List<Message> GetAllMessage(String UserName)
    {
        DataClassesDataContext db = new DataClassesDataContext();
        var messages = db.Community_Tables.Where(q => q.User_Table.Username.Equals(UserName) && q.Mess_Status == false);
        List<Message> messageList = new List<Message>();
        foreach(Community_Table message in messages)
        {
            messageList.Add(new Message(message));
        }
        return messageList;
    }
}
